package com.wxf.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxf.mapper.UserMapper;
import com.wxf.pojo.User;
import com.wxf.pojo.UserExample;
import com.wxf.util.VerifyCodeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public Integer getVerifyCode(){
        return VerifyCodeUtil.getVerifyCode();
    }

    public User verifyUser(String userName,String password){

        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUserLoginNameEqualTo(userName);
        List<User>userList = userMapper.selectByExample(example);
        if(userList.size()>0) {
            for (User user : userList) {
                if(user.getUserPassword().equals(password)){
                    user.setUserMenu(userMapper.findUserMenu(user.getRoleId()));
                    System.out.println(user);
                    return user;
                }
            }
        }
        return null;
    }

    public PageInfo getUserListByPage(Integer pageNum, Integer size){
        PageHelper.startPage(pageNum,size);
        List<User>userList = userMapper.getUserList();
        PageInfo pageInfo = new PageInfo(userList);
        return pageInfo;
    }
}
